﻿using Authentication.Core.Configuration;
using Authentication.Model.Interface;
using Authentication.Model.Model;
using Common.Contract;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Contract.SystemConfig.Record;
using Common.Data.Repository.Enum;
using Common.Service.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Authentication.Core.ServiceCommand
{
    public class UpdateStatusRequestDaoServiceCommand : ProviderServiceCommand<IProviderFactory, UpdateCreateDaoRequestRequest, ResponseRecord>
    {
        public UpdateStatusRequestDaoServiceCommand(IProviderFactory providers) : base(providers) { }
        #region Private Properties
        private UserRecord currentUser;
        #endregion
        protected override void Validate(UpdateCreateDaoRequestRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("User record must be set", "request");
            }
            else
            {
                currentUser = request.CurrentUser;
            }
            if (request.Record == null || request.Record.RequestId == 0 || request.Record.StatusCreateDaoRequest == 0)
            {
                throw new ArgumentException("Record must be set", "request");
            }
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var record = Request.Record;
            bool isPermission = false;
            bool savedUser = false;
            try
            {
                var checkUser = Providers.RequestDao.RetrieveRequestByRequestId(record.RequestId, Request.CurrentUser);
                if (record.StatusCreateDaoRequest == (int)StatusRequestCreateDaoEnum.Reject && (currentUser.GroupIdentifier.Equals("4") || currentUser.GroupIdentifier.Equals("6")))
                {
                    if (checkUser != null && checkUser.Pic.Equals(currentUser.UserName))
                    {
                        isPermission = true;
                    }
                }

                if (currentUser.GroupIdentifier.Equals("3") || currentUser.GroupIdentifier.Equals("2") || SystemConfiguration.UserMBDApprovedDao.Contains(currentUser.UserName) || SystemConfiguration.UserBICApprovedDao.Contains(currentUser.UserName))
                {
                    isPermission = true;
                }

                if (isPermission)
                {
                    if (checkUser != null)
                    {
                        if (record.StatusCreateDaoRequest == (int)StatusRequestCreateDaoEnum.Approve)
                        {
                            switch (currentUser.GroupIdentifier)
                            {
                                case "3":
                                    checkUser.Status = (int)StatusRequestCreateDaoEnum.BranchApproved;
                                    break;
                                case "2":
                                    checkUser.Status = (int)StatusRequestCreateDaoEnum.RegionApproved;
                                    break;
                                case "1":
                                    if (SystemConfiguration.UserMBDApprovedDao.Contains(currentUser.UserName))
                                    {
                                        checkUser.Status = (int)StatusRequestCreateDaoEnum.RegionApproved;
                                    }
                                    break;
                                default:
                                    break;
                            }
                        }
                        else
                        {
                            checkUser.Status = record.StatusCreateDaoRequest;
                        }
                        if (record.StatusCreateDaoRequest == (int)StatusRequestCreateDaoEnum.Reject)
                        {
                            checkUser.RejectPerson = record.RejectPerson;
                            checkUser.GroupIdReject = int.Parse(currentUser.GroupIdentifier);
                        }
                        savedUser = Providers.RequestDao.UpdateStatusRequestCreateDao(checkUser);
                    }
                    else
                    {
                        response.ErrorMessage = string.Format("Request is invalid!");
                    }
                }
                else
                {
                    response.ErrorMessage = string.Format("User {0} not have permission", currentUser.UserName);
                }
                //bool savedUser = Providers.RequestDao.SaveDaoRequest(userQuestion);
                //response.IsSuccess = savedUser;
            }
            catch (Exception ex)
            {
                response.ErrorMessage = ex.Message;
            }
            return savedUser;
        }
    }
}
